local hx__weisi = fk.CreateSkill {
  name = "hx__weisi",
  tags = { Skill.AttachedKingdom },
  attached_kingdom = {"jin"},
}

local HX = require "packages/huixiangkill/hxfs"

Fk:loadTranslationTable{
  ["$hx__weisi"] = "威肆",
  ["#hx__weisi"] = "威肆：令一名角色将任意张手牌移出游戏直到回合结束，然后视为对其使用【决斗】！",
  ["#hx__weisi-ask"] = "威肆：%src 将对你使用【决斗】！请将任意张手牌本回合移出游戏，【决斗】对你造成伤害后其获得你所有手牌！",
}

hx__weisi:addEffect("active", {
  name = "hx__weisi",
  mute = true,
  prompt = "#hx__weisi",
  card_num = 0,
  target_num = 1,
  can_use = function(self, player)
    return player:hasSkill(hx__weisi.name)
  end,
  card_filter = Util.FalseFunc,
  target_filter = function(self, player, to_select, selected, selected_cards)
    return #selected == 0 and to_select ~= player
  end,
  on_use = function(self, room, effect)
    local player = effect.from
    local target = effect.tos[1]
    player:broadcastSkillInvoke(hx__weisi.name, math.random(1, 2))
    room:notifySkillInvoked(player, hx__weisi.name, "offensive")
    local cards = room:askToCards(target, {
      min_num = 1,
      max_num = 999,
      skill_name = hx__weisi.name,
      prompt = "#weisi-ask:"..player.id,
      cancelable = true,
    })
    local ids = {}
    for _, i in ipairs(room.alive_players) do
      table.insert(ids, i.id)
    end
    if #cards > 0 then
      player:addToPile("$hx__weisi", cards, false, hx__weisi.name, player, ids)
      if #target:getCardIds("h") == 0 then
        room:damage({
          from = player,
          to = target,
          damage = 1,
          skillName = hx__weisi.name,
        })
        room:notifySkillInvoked(player, hx__weisi.name, "switch")
        room:changeKingdom(player, "wei", true)
        if player.general == "hxjsctB2__simazhao" or player.deputyGeneral == "hxjsctB2__simazhao" then
          HX.ChangeGeneral(room, player, "hxjsctB2__simazhao", "hxjsctB__simazhao")
        end
        room:setPlayerMark(player, "hx__weisi", 0)
      end
    end
    if player.dead or target.dead then return end
    local card = Fk:cloneCard("duel")
    card.skillName = hx__weisi.name
    if not player:isProhibited(target, card) then
      local use = {
        from = player,
        tos = {target},
        card = card,
        extra_data = {
          weisi_to = target,
        },
      }
      room:useCard(use)
    end
    if player.kingdom ~= "wei" then
      room:invalidateSkill(player, hx__weisi.name, "-turn")
    end
  end,
})

hx__weisi:addEffect(fk.Damage, {
  mute = true,
  is_delay_effect = true,
  can_trigger = function(self, event, target, player, data)
    if target == player and not player.dead and
      data.card and table.contains(data.card.skillNames, hx__weisi.name) then
      local use_event = player.room.logic:getCurrentEvent():findParent(GameEvent.UseCard)
      if use_event then
        local use = use_event.data
        return use.extra_data and use.extra_data.weisi_to == data.to
      end
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    player:broadcastSkillInvoke(hx__weisi.name, 3)
    room:moveCardTo(data.to:getCardIds("h"), Card.PlayerHand, player, fk.ReasonPrey, hx__weisi.name, nil, false, player)
    room:notifySkillInvoked(player, hx__weisi.name, "switch")
    room:changeKingdom(player, "wei", true)
    if player.general == "hxjsctB2__simazhao" or player.deputyGeneral == "hxjsctB2__simazhao" then
      HX.ChangeGeneral(room, player, "hxjsctB2__simazhao", "hxjsctB__simazhao")
    end
    room:setPlayerMark(player, "hx__weisi", 0)
  end,
})



return hx__weisi
